<script>
	import {getBannerList, getReommendList, getUserInfo, getStoreList} from 'api/home/index.js';
	import {
		myCouponList
	} from 'api/me/index.js';
	import {getGoods} from 'api/order/index.js';
	import Vue from 'vue';
	import {mapActions} from 'vuex';
	import {setToken} from 'utils/auth.js';
	import {getDistance} from 'utils/index.js';
	export default {
		onLaunch: function() {
			const that = this;
			// 轮播图
			getBannerList().then(res => {
				this.setBannerList({bannerList: res.data});
			});
			// 商品
			getGoods().then(res => {
				this.setGoods({goodsList: res.data});
			});
			// 门店列表
			getStoreList().then(res => {
				let store = res.data[0];
				store.status = !!Number(store.status);
				uni.getLocation({
				    type: 'wgs84',
				    success: function (res) {
						let distance = getDistance(res.latitude, res.longitude, store.latitude, store.longitude)
						that.setStore({store: Object.assign(store, {distance})});
				    },
					fail() {
						that.setStore({store: Object.assign(store, {distance: '9999km'})});
					}
				});
				
			});
			
			uni.getSystemInfo({
				success(e) {
					Vue.prototype.scrollviewHeilght = e.safeArea.height;
				}
			});
			uni.login({
				provider: 'weixin',
				success(e) {
					let {
						code,
						errMsg
					} = e;
					if (errMsg = 'login:ok') {
						that.$slLoading('加载中...').then(() => {
							// 获取用户信息
							getUserInfo(code).then(res => {
								const {token, user: userInfo} = res;
								if(userInfo) {
									setToken(token);
									that.setUserInfo({userInfo});
									// 获取优惠券
									myCouponList().then(res => {
										that.setCouponList({couponList: res.rows});
									})
									// 获取桌子信息
									that.setStatusInfo({id: userInfo.id});
								}
							}).finally(() => {
								uni.hideLoading();
							})
						})
					}
				}
			})
		},
		onShow: function() {
		},
		onHide: function() {
		},
		methods: {
			...mapActions(['setBannerList', 'setUserInfo', 'setStore', 'setCategory', 'setGoods', 'setCouponList', 'setStatusInfo'])
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "static/css/index.css";
	// 引入vant样式
	@import "wxcomponents/vant/dist/common/index.wxss";
	// 引入iconfont
	@import "static/css/iconfont.css";
</style>
